const http = {
	//const baseUrl = 'http://127.0.0.1:8088',
		request(config) {
			config = beforeRequest(config)
			config.url = 'http://127.0.0.1:8088' + config.url
			return new Promise((resolve, reject) => {
				uni.request(config)
					.then(res => {
						let [error, resp] = res
						const response = beforeResponse(resp)
						resolve(response)
					})
					.catch(err => {
						errorHandle(err)
						reject(err)
					})
			})

		},
		get(url, data, auth) {
			/*
			url:接口地址
			data:请求体参数
			auth:请求是否需要携带token进行认证(true or false)
			*/
			return this.request({
				url: url,
				data: data,
				auth: auth,
				method: "GET"
			})
		},
		post(url, data, auth) {
			return this.request({
				url: url,
				data: data,
				auth: auth,
				method: "POST"
			})
		},
		delete(url, data, auth) {
			return this.request({
				url: url,
				data: data,
				auth: auth,
				method: "DELETE"
			})
		},
		put(url, data, auth) {
			return this.request({
				url: url,
				data: data,
				auth: auth,
				method: "PUT"
			})
		}

}
//请求拦截器
const beforeRequest = (config) => {

	//请求之前做的操作
	console.log('请求拦截器：', config)
	return config

}
//响应拦截器
const beforeResponse = (response) => {
	console.log('响应拦截器：', response)
	return response
}
//异常处理器
const errorHandle = (err) => {
	console.log('网络异常，请求失败!', err)
}
export default http